﻿using APPWG4806.Back.Entry;
using APPWG4806.Haper;
using APPWG4806.Helper;
using DevExpress.XtraEditors;
using DevExpress.XtraWaitForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebCarLine.Common;
using static DevExpress.Utils.HashCodeHelper.Primitives;

namespace APPWG4806.View
{
    public partial class LoginUserForm : DevExpress.XtraEditors.XtraForm
    {

        public static UserInfoEntry userInfo = null;

        public string sqlStr = "SELECT TOP ( 2 ) [u].[ID], [u].[AccountName], [u].[Condtion], [u].[CreateDate]," +
            " [u].[Creator], [u].[DepartmentId], [u].[Description], [u].[LoginId], [u].[Mail], " +
            "[u].[Modifier], [u].[ModifyDate], [u].[Telphone], [u].[UserIndex], [u].[UserPassword] FROM [User] AS [u] " +
            "WHERE [u].[LoginId] = @LoginId";

        public LoginUserForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string account = Account_TB.Text;
            string password = password_TB.Text;

            if (!string.IsNullOrEmpty(account)&&!string.IsNullOrWhiteSpace(password))
            {
                DataTable data = DatabaseHelper.ExecuteQuery(sqlStr, new List<SqlParameter> {
                    new SqlParameter("@LoginId",account) });
                 List<UserInfoEntry> users = APPWG4806.Helper.DataTableExtensions.ToList<UserInfoEntry>(data);
                bool pwExists = false;
                if (data.Rows.Count>0)
                {
                    string encodPassword = StringHelp.EncodeBase64(System.Text.Encoding.Default, password);
                    foreach (var item in users)
                    {
                        if (item.UserPassword.Equals(encodPassword)&&item.LoginId == account)
                        {
                            pwExists = true;
                            userInfo = item;
                        }
                    }
                    if (pwExists)
                    {
                        this.Hide();
                        
                        Form132 form = new Form132();
                        form.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("密码错误");
                    }
                }
                else
                {
                    MessageBox.Show("用户名未找到");
                }
            }
            else
            {
                MessageBox.Show("用户或密码不能为空！");
                return;
            }

        }

        private void password_TB_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // 触发登录按钮的点击事件
                button1_Click(sender, e);
            }
        }

        private void show_CB_CheckedChanged(object sender, EventArgs e)
        {
            // 如果 CheckBox 被选中，显示密码；否则隐藏密码
            password_TB.UseSystemPasswordChar = !show_CB.Checked;
        }
    }
}